<?php

/**
 * @Description    用户所属分公司(事业部)
 * @Author         zhaojiping
 * @Copyright      Copyright (c) 山东艾思软件科技有限公司(https://www.aisisofit.cn)
 * @Date           2022/2/7
 * @CreateBy       VS Code
 */

namespace app\core\model;

use think\Model;
use think\facade\Db;

class CoreRole extends \app\common\model\CommonModel {
    protected $fields = 'id, name, rules, usable, desc'; // 通用查询字段
    protected $order = 'sort, id'; // 排序字段

    public static function onAfterRead($row) {
        $coreRule = Db::name('CoreRule');
        if ($row['rules'])
            $row['rules_label'] = $coreRule->where('id', 'in', $row['rules'])->column('title');
    }

    public static function onBeforeDelete($row) {
        parent::onBeforeDelete($row);

        $result = Db::name('CoreUser')->where(['role_id' => $row['id']])->count();
        if ($result > 0)
            json_return(0, '有员工属于该角色, 请先删除或修改属于该角色的员工');
    }
}
